
//
//  HTKeyBoardView.swift
//  weibo
//
//  Created by JonsonLiu on 16/4/27.
//  Copyright © 2016年 liujonson. All rights reserved.
//

import UIKit

// 每一页有多少列
let HMEmoticonKetboardMaxCol = 7
// 每一页有多少行
let HMEmoticonKetboardMaxRow = 3
// 每一个页显示多少个表情按钮
let HMEmoticonKetboardPageEmticonCount = HMEmoticonKetboardMaxCol * HMEmoticonKetboardMaxRow - 1

class HTKeyBoardView: UIView {

   
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
    
//        backgroundColor = UIColor.blueColor()
        
        //backgroundColor = UIColor.grayColor()
         backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        toobar.emtionButtonClickClosed = { (type) -> () in
        
            print(type)
            let  indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            self.emotionCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            self.showPageControl(indexPath)
        }
        
        addSubview(toobar)
        addSubview(emotionCollectionView)
        addSubview(pageControl)
        
        toobar.snp_makeConstraints { (make) -> Void in
            make.leading.bottom.trailing.equalTo(self)
            make.height.equalTo(37)
        }
        
        emotionCollectionView.snp_makeConstraints { (make) -> Void in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(toobar.snp_top)
        }
        
        pageControl.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(toobar.snp_top)
            make.centerX.equalTo(toobar)
        }
        
        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            
            let indexPath = NSIndexPath(forItem: 0, inSection: 1)
            
            self.emotionCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Left, animated: false)
            
            self.showPageControl(indexPath)
        }
        
    }
    
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let  layout = emotionCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.itemSize = CGSize(width: UIScreen.mainScreen().bounds.width, height: self.frame.height - toobar.frame.height)
        
        
    }
    
    

    
 /// 添加pagecontrol
    private lazy var pageControl : UIPageControl = {
    
        let  control = UIPageControl()
        control.hidden = true
        
        control.currentPageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_selected")!)
        control.pageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_normal")!)
        
        control.userInteractionEnabled = false
        return control
    }()
    
    private lazy var toobar : HTEmoticonToolBar = HTEmoticonToolBar(frame: CGRectZero)
    
    private lazy var emotionCollectionView : UICollectionView = {
    
        let  layout = UICollectionViewFlowLayout()
        
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        layout.scrollDirection = .Horizontal
        
        let collectionView = UICollectionView(frame: CGRectZero,collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.clearColor()
        collectionView.registerClass(HTEmoticonPageCell.self, forCellWithReuseIdentifier: "cell")
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.bounces = false
        collectionView.pagingEnabled = true
        collectionView.showsHorizontalScrollIndicator = false
        
        return collectionView
    }()
    
}

extension HTKeyBoardView : UICollectionViewDataSource,UICollectionViewDelegate{

    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//        return 10
        return HTEmoticonKeyBoardViewModel.sharedViewModel.allEmotcions[section].count
    }
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return HTEmoticonKeyBoardViewModel.sharedViewModel.allEmotcions.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! HTEmoticonPageCell
        
//        cell.backgroundColor = RandomColor
        cell.indexPath = indexPath
        
        cell.emtiocons = HTEmoticonKeyBoardViewModel.sharedViewModel.allEmotcions[indexPath.section][indexPath.item]
        
        return cell
    }

    func scrollViewDidScroll(scrollView: UIScrollView) {
        var center = scrollView.center
        
        center.x = center.x + scrollView.contentOffset.x
        
        let cells = emotionCollectionView.visibleCells()
        
        let  indexPath : NSIndexPath
        
        if cells.count == 2 {
        
            let firstCell = cells.first!
            let lastCell = cells.last!
            
            if CGRectContainsPoint(firstCell.frame, center){
            
                indexPath = emotionCollectionView.indexPathForCell(firstCell)!
                
            }else{
                
                indexPath = emotionCollectionView.indexPathForCell(lastCell)!
            }
            
        }else{
        
            indexPath = emotionCollectionView.indexPathForCell(cells.first!)!
        }
        
        print(indexPath.section)
        
        toobar.selectSection = indexPath.section
        
        showPageControl(indexPath)
    }

    
    func showPageControl(indexPath : NSIndexPath){
        
        pageControl.numberOfPages = HTEmoticonKeyBoardViewModel.sharedViewModel.allEmotcions[indexPath.section].count
        
        pageControl.currentPage = indexPath.item
        
        pageControl.hidden = indexPath.section == 0
    }
}

